/**
 * @file wa_checkbox.c
 *
 */

 /*********************
 *      INCLUDES
 *********************/
#include "wa_conf.h"
#if USE_WA_CHECKBOX != 0
#include "wa_checkbox.h"

 /*********************
 *      DEFINES
 *********************/

 /**********************
 *      TYPEDEFS
 **********************/

 /**********************
 *  STATIC PROTOTYPES
 **********************/
static wa_obj_callback ancestor_callback;
static void wa_checkbox_callback(wa_sign* sign);
/**********************
 *  STATIC VARIABLES
 **********************/


 /**********************
 *      MACROS
 **********************/

 /**********************
 *   GLOBAL FUNCTIONS
 **********************/
 /**
 * checkbox module init
 */
void wa_checkbox_module_init(void)
{
}

/**
* create a checkbox
* @param par
* @return checkbox obj
*/
wa_obj* wa_checkbox_create(wa_obj* par)
{
	wa_obj* new_checkbox;
	wa_checkbox_ext* checkbox_ext;

	new_checkbox = wa_obj_create(par, NULL);
	if (new_checkbox == NULL)
	{
		return new_checkbox;
	}

	if (ancestor_callback == NULL)
	{
		ancestor_callback = wa_obj_get_callback(new_checkbox);
	}
	checkbox_ext = wa_obj_malloc_ext_attr(new_checkbox, sizeof(wa_checkbox_ext));
	if (checkbox_ext == NULL)
	{
		return NULL;
	}

	checkbox_ext->state = wa_nok;
	checkbox_ext->fore_color = WA_COLOR_FOREGROUND;
	checkbox_ext->back_color = WA_COLOR_BACKGROUND;
	checkbox_ext->edge_size = 1;
	checkbox_ext->interval = 1;

	wa_obj_set_callback(new_checkbox, wa_checkbox_callback);

	return new_checkbox;
}

/**
* checkbox set state 
* @param checkbox
* @param state
*/
void wa_checkbox_set_state(wa_obj* checkbox, wa_result state)
{
	wa_checkbox_ext* ext;
	ext = wa_obj_get_ext_attr(checkbox);
	if (ext->state != state)
	{
		ext->state = state;
		wa_wm_notify_parent(checkbox, WA_SIGNAL_NOTIFY_VALUE_CHANGED);
		wa_obj_invalidate(checkbox);
	}
}

/**
* checkbox change state
* @param checkbox
*/
void wa_checkbox_change_state(wa_obj* checkbox)
{
	wa_checkbox_ext* ext;
	ext = wa_obj_get_ext_attr(checkbox);
	ext->state = (ext->state == wa_ok) ? wa_nok : wa_ok;
	wa_wm_notify_parent(checkbox, WA_SIGNAL_NOTIFY_VALUE_CHANGED);
	wa_obj_invalidate(checkbox);
}

/**
* checkbox set fore color
* @param checkbox
* @param fore color
*/
void wa_checkbox_set_fore_color(wa_obj* checkbox, wa_gui_color color)
{
	wa_checkbox_ext* ext;
	ext = wa_obj_get_ext_attr(checkbox);
	ext->fore_color = color;
	wa_obj_invalidate(checkbox);
}

/**
* checkbox set back color
* @param checkbox
* @param back color
*/
void wa_checkbox_set_back_color(wa_obj* checkbox, wa_gui_color color)
{
	wa_checkbox_ext* ext;
	ext = wa_obj_get_ext_attr(checkbox);
	ext->back_color = color;
	wa_obj_invalidate(checkbox);
}

/**
* checkbox set edge size
* @param checkbox
* @param edge size
*/
void wa_checkbox_set_edge_size(wa_obj* checkbox, uint32_t size)
{
	wa_checkbox_ext* ext;
	ext = wa_obj_get_ext_attr(checkbox);
	ext->edge_size = size;
	wa_obj_invalidate(checkbox);
}

/**
* checkbox set interval
* @param checkbox
* @param interval
*/
void wa_checkbox_set_interval(wa_obj* checkbox, uint32_t interval)
{
	wa_checkbox_ext* ext;
	ext = wa_obj_get_ext_attr(checkbox);
	ext->interval = interval;
	wa_obj_invalidate(checkbox);
}

/**
* checkbox get state
* @param checkbox
* @return state
*/
wa_result wa_checkbox_get_state(wa_obj* checkbox)
{
	wa_checkbox_ext* ext;
	ext = wa_obj_get_ext_attr(checkbox);
	return ext->state;
}

/**
* checkbox get fore color
* @param checkbox
* @return fore color
*/
wa_gui_color wa_checkbox_get_fore_color(wa_obj* checkbox)
{
	wa_checkbox_ext* ext;
	ext = wa_obj_get_ext_attr(checkbox);
	return ext->fore_color;
}

/**
* checkbox get back color
* @param checkbox
* @return back color
*/
wa_gui_color wa_checkbox_get_back_color(wa_obj* checkbox)
{
	wa_checkbox_ext* ext;
	ext = wa_obj_get_ext_attr(checkbox);
	return ext->back_color;
}

/**
* checkbox get edge size
* @param checkbox
* @return edge size
*/
uint32_t wa_checkbox_get_edge_size(wa_obj* checkbox)
{
	wa_checkbox_ext* ext;
	ext = wa_obj_get_ext_attr(checkbox);
	return ext->edge_size;
}

/**
* checkbox get interval
* @param checkbox
* @return interval
*/
uint32_t wa_checkbox_get_interval(wa_obj* checkbox)
{
	wa_checkbox_ext* ext;
	ext = wa_obj_get_ext_attr(checkbox);
	return ext->interval;
}

/**********************
*   STATIC FUNCTIONS
**********************/
/**
* checkbox redraw functions
* @param obj
*/
static void _Paint(wa_obj* obj)
{
	uint32_t i;
	wa_checkbox_ext* ext;
	ext = wa_obj_get_ext_attr(obj);

	wa_mask_fillrect(obj->rect.x1, obj->rect.y1,
					wa_obj_get_width(obj),
					wa_obj_get_height(obj),
					&(obj->redraw_rect),
					ext->back_color);
	
	for (i = 0; i < ext->edge_size; i++)
	{
		wa_mask_drawrect(obj->rect.x1 + i, obj->rect.y1 + i,
						wa_obj_get_width(obj) - 2 * i,
						wa_obj_get_height(obj) - 2 * i,
						&(obj->redraw_rect),
						ext->fore_color);
	}
	if (ext->state == wa_ok)
	{
	
		int start_x, start_y;
		int width, high;
		
		width = wa_obj_get_width(obj) - 2 * ext->edge_size - 2 * ext->interval;
		high = wa_obj_get_height(obj) - 2 * ext->edge_size - 2 * ext->interval;
		start_x = obj->rect.x1 + ext->edge_size + ext->interval;
		start_y = obj->rect.y1 + ext->edge_size + ext->interval;

		wa_mask_fillrect(start_x, start_y,
						width,
						high,
						&(obj->redraw_rect),
						ext->fore_color);
	}

}

/**
 * img obj delete function
 * @param obj
 */
static void _Delete(wa_obj* obj)
{
}

/**
 * img obj callback function
 * @param sign
 */
static void wa_checkbox_callback(wa_sign* sign)
{
	int sign_id;
	wa_obj* obj;

	ancestor_callback(sign);
	sign_id = sign->sign_id;
	obj = sign->obj_dst;

	switch (sign_id)
	{
		case WA_SIGNAL_PAINT:
		{
			_Paint(obj);
			break;
		}
		case WA_SIGNAL_DELETE:
		{
			_Delete(obj);
			break;
		}
		case WA_SIGNAL_RELEASED:
		{
			wa_checkbox_change_state(obj);
			break;
		}
		default:
		{
			break;
		}
	}
}

#endif
